package restful

import (
	"encoding/json"
	"fmt"
	"time"

	myLogger "ZHONGYIHANGYAN/commUtils/logger"

	"github.com/go-resty/resty/v2"
)

var logErr, logInfo = myLogger.GetLogger("restfulManage")

type ResponseData struct {
	Code int         `json:"Code"`
	Msg  string      `json:"Msg"`
	Body interface{} `json:"Body"`
}

func Get(url string) error {

	client := resty.New().SetTimeout(1 * time.Second)

	resp, err := client.R().Get(url)
	if err != nil {
		logErr.Println(err)
		return fmt.Errorf("发送请求失败")
	}
	if resp.StatusCode() == 200 {
		dataStr := resp.String()
		reData := ResponseData{}
		if err := json.Unmarshal([]byte(dataStr), &reData); err != nil {
			logErr.Println(err)
			return fmt.Errorf("响应结果解析失败")
		}
		if reData.Code == 200 {
			return nil
		}
	} else {
		return fmt.Errorf("请求响应失败")
	}
	return nil

}

func Delete(url string) error {

	client := resty.New().SetTimeout(1 * time.Second)

	resp, err := client.R().Delete(url)
	if err != nil {
		logErr.Println(err)
		return fmt.Errorf("发送请求失败")
	}
	if resp.StatusCode() == 200 {
		dataStr := resp.String()
		reData := ResponseData{}
		if err := json.Unmarshal([]byte(dataStr), &reData); err != nil {
			logErr.Println(err)
			return fmt.Errorf("响应结果解析失败")
		}
		if reData.Code == 200 {
			return nil
		}
	} else {
		return fmt.Errorf("请求响应失败")
	}
	return nil

}

/*
发送Post请求
*/
func Post(url string, requestBody interface{}) (interface{}, error) {

	client := resty.New().SetTimeout(10 * time.Second) //TODO 暂时加长响应超时时间
	// protocol, err := json.Marshal(requestBody)
	// if err != nil {
	// 	logErr.Println(err)
	// }
	// logInfo.Println("准备下发协议:", url, "   ", string(protocol))
	resp, err := client.R().SetBody(requestBody).Post(url)
	if err != nil {
		logErr.Println(err)
		return "", fmt.Errorf("发送请求失败")
	}
	if resp.StatusCode() == 200 {
		dataStr := resp.String()
		reData := ResponseData{}
		if err := json.Unmarshal([]byte(dataStr), &reData); err != nil {
			logErr.Println(err)
			return "", fmt.Errorf("响应结果解析失败")
		}
		if reData.Code == 200 {
			return reData.Body, nil
		}
	} else {
		return "", fmt.Errorf("请求响应失败")
	}
	return "", nil

}

// POST application/x-www-form-urlencoded
type LecBaseResponse struct {
	Status  int64       `json:"status"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

func Post_Formurlencoded(url string, formData map[string]string) (interface{}, error) {

	client := resty.New().SetTimeout(1 * time.Second)
	resp, err := client.R().SetHeader("Content-Type", "application/x-www-form-urlencoded").SetFormData(formData).Post(url)

	if err != nil {
		logErr.Println(err)
		return nil, fmt.Errorf("发送请求失败")
	}
	if resp.StatusCode() == 200 {
		reData := LecBaseResponse{}
		if err := json.Unmarshal(resp.Body(), &reData); err != nil {
			logErr.Println(err)
			return nil, fmt.Errorf("响应结果解析失败")
		}
		if reData.Status == 0 {
			return reData.Data, nil
		}
	} else {
		return nil, fmt.Errorf("请求响应失败")
	}
	return nil, nil

}

func Put(url string, requestBody interface{}) (interface{}, error) {
	client := resty.New().SetTimeout(1 * time.Second)
	// protocol, err := json.Marshal(requestBody)
	// if err != nil {
	// 	logErr.Println(err)
	// }
	// logInfo.Println("准备下发协议:", string(protocol))
	resp, err := client.R().SetBody(requestBody).Put(url)
	if err != nil {
		logErr.Println(err)
		return "", fmt.Errorf("发送请求失败")
	}
	if resp.StatusCode() == 200 {
		dataStr := resp.String()
		reData := ResponseData{}
		if err := json.Unmarshal([]byte(dataStr), &reData); err != nil {
			logErr.Println(err)
			return "", fmt.Errorf("响应结果解析失败")
		}
		if reData.Code == 200 {
			return reData.Body, nil
		}
	} else {
		return "", fmt.Errorf("请求响应失败")
	}
	return "", nil

}
